{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# C++的数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 整形"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 作用：整型变量表示的是整数类型的数据。\n",
    "\n",
    "② C++中能够表示整形的类型有以下几种方式，区别在于所占内存空间不同。\n",
    "\n",
    "③ 数据类型 占用空间 取值范围\n",
    "\n",
    "1. short(短整型) 2字节 -2^15-2^15-1\n",
    "2. int（整型） 4字节 -2^31-2^31-1\n",
    "3. long(长整型） Windows为4字节，Linux为4字节(32位)，8字节(64位) -2^31-2^31-1\n",
    "4. long long(长长整型) 8字节 -2^63-2^63-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //1、短整型(-32768~32767)\n",
    "    short num1 = 10;\n",
    "    short num5 = 32768; //溢出了，打印出来是-23768\n",
    "    short num6 = 32770; //溢出了，打印出来是-32766\n",
    "\n",
    "    //2、整型\n",
    "    int num2 = 10;\n",
    "\n",
    "    //3、长整型\n",
    "    long num3 = 10;\n",
    "\n",
    "    //4、长长整型\n",
    "    long long num4 = 10;\n",
    "\n",
    "    cout << \"num1 = ：\" << num1 << endl;\n",
    "    cout << \"num2 = ：\" << num2 << endl;\n",
    "    cout << \"num3 = ：\" << num3 << endl;\n",
    "    cout << \"num4 = ：\" << num4 << endl;\n",
    "    cout << \"num5 = ：\" << num5 << endl;\n",
    "    cout << \"num6 = ：\" << num6 << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - num1 = ：10  \n",
    " - num2 = ：10  \n",
    " - num3 = ：10  \n",
    " - num4 = ：10  \n",
    " - num5 = ：-32768  \n",
    " - num6 = ：-32766  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 实型(浮点型)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 作用：用于表示小数。\n",
    "\n",
    "② 浮点型变量分为下面两种，两个的区别在于表示有效数字范围不同。\n",
    "\n",
    "1. 单精度float\n",
    "2. 双精度double\n",
    "\n",
    "③ 数据类型 占用字节 有效数字范围\n",
    "\n",
    "1. float 4字节 7位有效数字\n",
    "2. double 8字节 15-16位有效数字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.1 单精度、双精度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //1、单精度  float\n",
    "    //2、双精度  double\n",
    "    //默认情况下，无论单精度还是双精度，都只会显示6位有效数字\n",
    "    float f1 = 3.1415926;  //默认为双精度，前面加float相当于把双精度转换为单精度\n",
    "    float f2 = 3.1415926f;  //后面加一个f，直接创建的是单精度\n",
    "\n",
    "    cout << \"f1 = ：\" << f1 << endl;\n",
    "\n",
    "    double d1 = 3.1415926;\n",
    "\n",
    "    cout << \"d1 = ：\" << d1 << endl;\n",
    "\n",
    "    cout << \"float  占用内存空间为\" << sizeof(float) << endl;\n",
    "    cout << \"double 占用内层空间为\" << sizeof(double) << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - f1 = ：3.14159  \n",
    " - d1 = ：3.14159  \n",
    " - float  占用内存空间为4  \n",
    " - double 占用内层空间为8  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.2 科学计数法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //科学计数法\n",
    "    float f3 = 3e2;  // 3 * (10 ^ 2)\n",
    "    cout << \"f3 = ：\" << f3 << endl;\n",
    "\n",
    "    float f4 = 3e-2; // 3 * (0.1 ^ 2)\n",
    "    cout << \"f4 = ：\" << f4 << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - f3 = ：300  \n",
    " - f4 = ：0.03  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3 字符型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 字符型变量用于显示单个字符。\n",
    "\n",
    "② 语法：char ch = 'a';\n",
    "\n",
    "③ 在显示字符型变量时，用单引号字符括起来，不要用双引号。\n",
    "\n",
    "④ 单引号内只能有一个字符，不可以是字符串。\n",
    "\n",
    "⑤ C和C++中字符型变量只占用1个字节。\n",
    "\n",
    "⑥ 字符型变量并不是把字符本身放到内存中存储，而是将对应的ASCII编码放入到存储单元。\n",
    "\n",
    "⑦ ASCII码大致由以下两部分组成：\n",
    "\n",
    "1. ASCII非打印控制字符：ASCII表上的数字0-31分配给了控制字符，用于控制像打印机等一些外围设备。\n",
    "2. ASCII打印字符：数字21-126分配给了能在键盘上找到的字符，当查看或打印文档时就会出现。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "\n",
    "    //1、字符型变量创建方式\n",
    "    char ch = 'a';\n",
    "    cout << ch << endl;\n",
    "\n",
    "    //2、字符型变量所占内存大小\n",
    "    cout << \"char字符型变量所占内存：\" << sizeof(char) << endl;\n",
    "\n",
    "    //3、字符型变量常见错误\n",
    "    char ch2 = 'b';  //创建字符型变量时候，要用单引号\n",
    "    //char ch3 = \"b\";  //错误，创建字符型变量时候，不能用双引号\n",
    "    //char ch4 = 'abcdef'; //错误，创建字符型变量时候，单引号内只能有一个字符\n",
    "\n",
    "    //4、字符型变量对应ASCII编码\n",
    "    cout << (int)ch << endl;   // (int)ch 为字符'a'的ASCIII码值\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - a  \n",
    " - char字符型变量所占内存：1  \n",
    " - 97  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.4 字符串型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 用于表示一串字符。\n",
    "\n",
    "② 字符串型有两种风格\n",
    "\n",
    "③ C风格字符串：char 变量名[] = \"字符串值\"\n",
    "\n",
    "④ C++风格字符串：string 变量名 = \"字符串值\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "#include <string> //用C++风格字符串时候，要包含这个头文件\n",
    "\n",
    "int main()\n",
    "{\n",
    "\n",
    "    //1、C风格字符串\n",
    "    //char 字符   =    这是一个字符\n",
    "    //char 字符[] =    这是一个字符串\n",
    "    //表示字符串时，等号后面，要用双引号\n",
    "\n",
    "    char str[] = \"hello world\";\n",
    "    cout << str << endl;\n",
    "\n",
    "    //2、C++风格字符串\n",
    "    string str2 = \"hello world\";\n",
    "    cout << str2 << endl;\n",
    "    \n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - hello world  \n",
    " - hello world  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.5 布尔类型 bool"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 布尔数据类型代表真或假的值。\n",
    "\n",
    "② bool类型只有两个值：\n",
    "\n",
    "1. true -- 真 (本质是1)\n",
    "2. false -- 假(本质是0)\n",
    "\n",
    "③ bool类型占1个字节大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "\n",
    "    //1、创建bool数据类型\n",
    "\n",
    "    bool flag1 = true; //true代表真\n",
    "    cout << flag1 << endl;\n",
    "    \n",
    "    bool flag2 = false; //flag 代表假\n",
    "    cout << flag2 << endl;\n",
    "    \n",
    "    //本质上 1代表真 0代表假\n",
    "\n",
    "    //2、查看bool类型所占空间\n",
    "    cout << \"bool所占内存空间：\" << sizeof(flag1) << endl;\n",
    "    cout << \"bool所占内存空间：\" << sizeof(flag2) << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 1  \n",
    " - 0  \n",
    " - bool所占内存空间：1  \n",
    " - bool所占内存空间：1  \n",
    " - 请按任意键继续. . ."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
